package scct.hys.dao;

import scct.hys.model.Movie;
import scct.hys.model.User;
import scct.hys.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MovieDao {


    public  List<Movie> getAllMovies() {

        List<Movie> list = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select * from cm_movie";
        PreparedStatement pstmt = null;
        try {
            pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                String director=rs.getString("director");
                String actors=rs.getString("actor");
                String releasedate= rs.getString("releasedate");
                String duration=rs.getString("duration");
                list.add(new Movie(id,name,director,actors,releasedate,duration));
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }



    public boolean addMovie( String name, String director, String actor, String releasedate, String duration){
//        System.out.println(name);
//        System.out.println(director);
//        System.out.println(actor);
//        System.out.println(releasedate);
//        System.out.println(duration);
        int result=0;
        Connection conn=DBUtils.getConnection();
        String insert_sql="insert into cm_movie values(null,?,?,?,?,?)";
        try {
            PreparedStatement pstmt=conn.prepareStatement(insert_sql);
            pstmt.setString(1,name);
            pstmt.setString(2,director);
            pstmt.setString(3,actor);
            pstmt.setString(4,releasedate);
            pstmt.setString(5,duration);
            result=pstmt.executeUpdate();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return result>0;
    }
    public boolean updateMovie(int id,String name,String director,String actor,String releasedate,String duration){

        int count=0;
        Connection conn=DBUtils.getConnection();
        String update_sql="update cm_movie set name=?,director=?,actor=?,releasedate=?,duration=? where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(update_sql);
            pstmt.setString(1,name);
            pstmt.setString(2,director);
            pstmt.setString(3,actor);
            pstmt.setString(4,releasedate);
            pstmt.setString(5,duration);
            pstmt.setInt(6,id);
            count=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return count>0;
    }
    public  Movie getMovieById(int id){
        Movie movie=null;
        Connection conn=DBUtils.getConnection();
        String sql="select * from cm_movie where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs=pstmt.executeQuery();
            if (rs.next()){
                String name=rs.getString("name");
                String director=rs.getString("director");
                String actor=rs.getString("actor");
                String releasedate=rs.getString("releasedate");
                String duration=rs.getString("duration");
                movie=new Movie(id,name,director,actor,releasedate,duration);

            }
            DBUtils.closeConnection(conn);

        }catch (SQLException e){
            e.printStackTrace();
        }
        return movie;
    }
    public boolean delMovie(int id) {
        System.out.println("id:"+id);
        int num=0;
        Connection conn=DBUtils.getConnection();
        String del_sql="delete from cm_movie where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(del_sql);
            pstmt.setInt(1,id);
            num=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return num>0;

    }
    public ArrayList<Movie> getMovieByName(String director) {
        ArrayList<Movie> movies=new ArrayList<>();
        Connection conn=DBUtils.getConnection();

        String sql="select * from cm_movie where director like '%"+director +"%' ";
        try{
            PreparedStatement pstmt=conn.prepareStatement(sql);

            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String mname=rs.getString("name");
                String mdirector=rs.getString(3);
                String actor=rs.getString("actor");
                String releaseDate=rs.getString("releaseDate");
                String duration=rs.getString("duration");
                movies.add(new Movie(id,mname,mdirector,actor,releaseDate,duration));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return  movies;


    }


}
